package com.google.code.sip.ucenter.web.admin.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.sip.core.dao.Page;
import com.google.code.sip.core.utils.WebGridUtils;
import com.google.code.sip.ucenter.model.Account;
import com.google.code.sip.ucenter.model.AccountLog;
import com.google.code.sip.ucenter.model.User;
import com.google.code.sip.ucenter.service.AccountLogManager;
import com.google.code.sip.ucenter.service.AccountManager;
import com.google.code.sip.ucenter.service.UserManager;

@Controller
public class HuoshowAccountLogController {

	@Autowired
	private AccountLogManager huoshowAccountLogManager;
	@Autowired
	private UserManager userManager;

	@Autowired
	private AccountManager huoshowAccountManager;

	@RequestMapping("/admin/huoshowAccountLog.do")
	public ModelAndView list(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String, Object>();
		Page<AccountLog> page = new Page<AccountLog>();
		WebGridUtils.fillPage(request, page);

		String username = request.getParameter("username");
		String email = request.getParameter("email");
		String huoshowId = request.getParameter("huoshowId");
		boolean hasUserName = StringUtils.isNotBlank(username);
		boolean hasEmail = StringUtils.isNotBlank(email);
		boolean hasHuoshowId = StringUtils.isNotBlank(huoshowId);
		if (hasUserName || hasEmail || hasHuoshowId) {
			User user = userManager.getUser(username.trim(), email.trim(),
					huoshowId.trim());
			if (user != null) {
				Account huoshowAccount = huoshowAccountManager
						.getLogByUser(user);
				if (huoshowAccount != null) {
					page.addFilter("huoshowAccount", huoshowAccount);
					page = huoshowAccountLogManager.getHuoshowAccountLog(page);
					model.put("totalRows", (int) page.getTotalCount());
				} else {
					model.put("totalRows", 0);
				}
			} else {
				model.put("totalRows", 0);
			}
		} else {
			page = huoshowAccountLogManager.getHuoshowAccountLog(page);
			model.put("totalRows", (int) page.getTotalCount());
		}
		model.put("page", page);
		return new ModelAndView("admin.huoshowAccountLog.list", model);
	}
}
